package com.zhao.uitest.chat_ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.zhao.uitest.BaseActivity;
import com.zhao.uitest.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zhao on 2016/2/22.
 */
public class ChatActivity extends BaseActivity {
    private List<Msg> msgList;
    private ListView listView;
    private Button bt_send ;
    private EditText et_text ;
    private MsgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_layout);
        init();
        initMsg();

    }

    private void init(){
        msgList = new ArrayList<Msg>();
        listView = (ListView) findViewById(R.id.lv_msg);
        bt_send = (Button) findViewById(R.id.bt_send);
        et_text = (EditText) findViewById(R.id.et_input_text);

        adapter = new MsgAdapter(ChatActivity.this, R.layout.msg_item, msgList);
        listView.setAdapter( adapter);

        bt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sendText = et_text.getText().toString();
                msgList.add(new Msg(sendText,Msg.TYPE_SENT));
                initMsg();
                adapter.notifyDataSetChanged();
                listView.setSelection(msgList.size());
                et_text.setText("");
            }
        });
    }
    private void initMsg(){
        if (msgList.size()>5)
            return;
        //模拟三次回复消息
        switch (msgList.size()){
            case 1:
                Msg msg1 = new Msg("Hi", Msg.TYPE_RECEIVED);
                msgList.add(msg1);
                return;
            case 3:
                Msg msg2 = new Msg("I am fine", Msg.TYPE_RECEIVED);
                msgList.add(msg2);
                return ;
            case 5:
                Msg msg3 = new Msg("Yes", Msg.TYPE_RECEIVED);
                msgList.add(msg3);
                return;
        }
    }
}
